package com.hbck.bspatch

import android.content.Context
import android.content.Intent
import android.net.Uri
import android.os.Environment
import android.util.Log
import java.io.File

/**
 * @author  JianQiang Ding
 * @Date    2018-04-23.
 */
val PATCH_FILE = "apk.patch"
val URL_PATH_DOWNLOAD = "http://39.106.211.160:8080/web-runner/UpdateFile/" + PATCH_FILE
val PACKAGE_NAME = "com.hbck.bspatch"
val SD_CARD = Environment.getExternalStorageDirectory().absolutePath + File.separator
val NEW_APK_PATH = SD_CARD + "dn_apk_new.apk"
val PATCH_FILE_PATH = SD_CARD + PATCH_FILE

fun Context.getVersionCode(): Int {
    val packageManager = this.packageManager
    val packageInfo = packageManager.getPackageInfo(this.packageName, 0)
    Log.d("tim", "versionCode = ${packageInfo.versionCode}")
    return packageInfo.versionCode
}

//获取当前data/app/ 源文件的路径
fun Context.getApkSourceDir(packageName: String): String {
    val appInfo = this.packageManager.getApplicationInfo(packageName, 0)
    return appInfo.sourceDir
}

//安装apk
fun Context.installApk(apkPath: String) {
    val intent = Intent(Intent.ACTION_VIEW)
    intent.setDataAndType(Uri.parse("file://" + apkPath), "application/vnd.android.package-archive")
    this.startActivity(intent)
}
